package cn.wolfcode;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class HttpClientSpider {

	/**
     * @param
	 */
	public void getHtml(){
		try {  
			   HttpClient httpClient = new HttpClient();// https://www.shicimingju.com/chaxun/list/4059.html https://www.shicimingju.com/chaxun/list/3050.html
			   GetMethod getMethod = new GetMethod("https://www.shicimingju.com/chaxun/list/4059.html");
			  //创建一个输出流,将爬到的小说以txt形式保存在硬盘
			  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\chenjiwei\\白居易琵琶行.txt")));

			   //获取状态码
	           int statusCode = httpClient.executeMethod(getMethod); 
	           System.out.println(getMethod.getStatusCode());
	           
	           if (statusCode == HttpStatus.SC_OK) {  
	        	   byte[] responseBody = getMethod.getResponseBody();
	  			   System.out.println(new String(responseBody));  
	           } else if((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) || 
				(statusCode == HttpStatus.SC_MOVED_PERMANENTLY)){
	        	   //发生重定向后的处理
	        	   Header redirect = getMethod.getResponseHeader("location");

		           String url = redirect.getValue();

		           getMethod = new GetMethod(url);
		           httpClient.executeMethod(getMethod);


		           // 读取内容
		           byte[] responseBody = getMethod.getResponseBody();
		           // 处理内容
		           String html = new String(responseBody);
				   //3. 解析数据:
				   Document document = Jsoup.parse(html);


				   //获得本章的章节名称 并输出到文本中
				   Elements chapterName = document.select(".j_chapterName");
				   System.out.println(chapterName.text());

				   System.out.println("============================================");

				   Element title = document.getElementById(".zs_title");
				   String zstitle = title.text();
				   System.out.println(zstitle);

				   Element zuozhe = document.getElementById(".niandai_zuozhe");
				   String niandaizuozhe = zuozhe.text();
				   System.out.println(niandaizuozhe);

				   Element xu = document.getElementById(".zs_xu");
				   String zsxu = xu.text();
				   System.out.println(zsxu);

				   Element content = document.getElementById(".zs_content");
				   String zscontent = content.text();
				   System.out.println(zscontent);


				   bw.write(title.text());
				   bw.write(zuozhe.text());
				   bw.write(xu.text());
				   bw.write(content.text());
				   bw.newLine();
				   bw.flush();

				   //获得本章的小说内容 并输出到文本中
				   Elements pEl = document.select("[class=item_content] p");
				   for (Element p : pEl) {
					   bw.write(p.text());
					   System.out.println(p.text());
					   bw.newLine();
					   bw.flush();
				   }
		           System.out.println(pEl);
		           System.out.println(html);
	           }else{
	        	   System.out.println("暂时无法读取");
	           }
	           
	       } catch (Exception e) {  
	           System.err.println("页面无法访问");  
	       }
	  }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HttpClientSpider hcs = new HttpClientSpider();
		hcs.getHtml();
	}
}